package com.yanqu.road.server.cmd.tradewar;

import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.tradewar.UserTradeWarBuilding;
import com.yanqu.road.pb.tradewar.TradeWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.TradeWarBuildingModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;


@Cmd(code = Protocol.S_TRADE_WAR_BUILDING_GET_REWARD, desc = "商战建筑领取收益")
public class BuildingGetRewardCmd extends AbstractCommand {

	private int type;

	private Property reward;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		TradeWarProto.TradeWarBuildingGetRewardReqMsg reqMsg = TradeWarProto.TradeWarBuildingGetRewardReqMsg.parseFrom(packet.getBytes());
		type = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		TradeWarBuildingModule tradeWarBuildingModule = player.getModule(TradeWarBuildingModule.class);
		UserTradeWarBuilding userTradeWarBuilding = tradeWarBuildingModule.getUserTradeWarBuilding(type);
		if(null == userTradeWarBuilding){//商战建筑未解锁
			ret = GameErrorCode.E_TRADE_WAR_BUILDING_IS_LOCK;
			return;
		}
		long nowTime = System.currentTimeMillis() / 1000;
		if(nowTime - userTradeWarBuilding.getLastGetTime() < GameConfig.TRADE_WAR_BANK_GET_SILVER_MIN_TIME){//领取太频繁
			ret = GameErrorCode.E_TRADE_WAR_BANK_GET_SILVER_TIME_OFTEN;
			return;
		}
		//领取收益
		reward = tradeWarBuildingModule.getBuildingReward(type, nowTime, false);
	}

	@Override
	public void sendData(GamePlayer player) {
		TradeWarProto.TradeWarBuildingGetRewardRespMsg.Builder respMsg = TradeWarProto.TradeWarBuildingGetRewardRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			respMsg.setReward(PropertyHelper.parsePropertyToString(reward));
		}
		player.sendPacket(Protocol.U_TRADE_WAR_BUILDING_GET_REWARD, respMsg);
	}
}
